package com.dms.modules.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.product.service.ProductCommentService;
import com.dms.modules.product.vo.CommentVO;
import com.dms.modules.product.vo.CommentStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 商品评论基础控制器（公共访问）
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/products/comments")
@Api(tags = "商品评论接口")
public class ProductCommentController {
    
    @Autowired
    private ProductCommentService commentService;

    @GetMapping("/check-reviewed")
    @ApiOperation("检查商品是否已评价")
    @PreAuthorize("hasAnyRole('USER', 'ADMIN', 'MERCHANT')")
    public ApiResponse<Boolean> checkHasReviewed(
            @ApiParam("订单ID") @RequestParam @NotNull(message = "订单ID不能为空") Long orderId,
            @ApiParam("商品ID") @RequestParam @NotNull(message = "商品ID不能为空") Long productId) {
        int reviewCount = commentService.checkHasReviewed(orderId, productId);
        return ApiResponse.success(reviewCount > 0);
    }

    @GetMapping("/product/{productId}")
    @ApiOperation("获取商品评论列表")
    public ApiResponse<IPage<CommentVO>> getProductComments(
            @ApiParam("商品ID") @PathVariable @NotNull(message = "商品ID不能为空") Long productId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize) {
        Page<CommentVO> page = new Page<>(pageNum, pageSize);
        IPage<CommentVO> comments = commentService.getProductComments(productId, page);
        return ApiResponse.success(comments);
    }
    // @Data
    // public static class AddCommentRequest {
    //     @NotNull(message = "订单ID不能为空")
    //     private Long orderId;
        
    //     @NotNull(message = "商品ID不能为空")
    //     private Long productId;
        
    //     @NotBlank(message = "评论内容不能为空")
    //     private String content;
        
    //     @NotNull(message = "评分不能为空")
    //     @Min(value = 1, message = "评分不能小于1")
    //     private Integer rating;
        
    //     private List<String> images;
        
    //     private Boolean isAnonymous;
    // }
    
    // @PostMapping
    // @ApiOperation("添加评论")
    // @PreAuthorize("hasRole('USER')")
    // public ApiResponse<Long> addComment(@RequestBody @Validated AddCommentRequest request) {
    //     Long userId = SecurityUtils.getCurrentUserId();
    //     // 验证订单状态和商品是否属于该订单
    //     Long commentId = commentService.addComment(userId, request.getOrderId(), 
    //         request.getProductId(), request.getContent(), request.getRating(), 
    //         request.getImages(), request.getIsAnonymous());
    //     return ApiResponse.success(commentId);
    // }
    
    // @DeleteMapping("/{commentId}")
    // @ApiOperation("删除评论")
    // @PreAuthorize("hasRole('USER')")
    // public ApiResponse<Boolean> deleteComment(
    //         @ApiParam("评论ID") @PathVariable @NotNull(message = "评论ID不能为空") Long commentId) {
    //     Long userId = SecurityUtils.getCurrentUserId();
    //     boolean success = commentService.deleteComment(commentId, userId);
    //     return ApiResponse.success(success);
    // }
    

    
    // @GetMapping("/my")
    // @ApiOperation("获取我的评论列表")
    // @PreAuthorize("hasRole('USER')")
    // public ApiResponse<IPage<CommentVO>> getMyComments(
    //         @ApiParam("页码") @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
    //         @ApiParam("每页数量") @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize) {
    //     Long userId = SecurityUtils.getCurrentUserId();
    //     Page<CommentVO> page = new Page<>(pageNum, pageSize);
    //     IPage<CommentVO> comments = commentService.getUserComments(userId, page);
    //     return ApiResponse.success(comments);
    // }
    
    // @GetMapping("/{commentId}")
    // @ApiOperation("获取评论详情")
    // public ApiResponse<CommentVO> getCommentDetail(
    //         @ApiParam("评论ID") @PathVariable @NotNull(message = "评论ID不能为空") Long commentId) {
    //     CommentVO comment = commentService.getCommentDetail(commentId);
    //     return ApiResponse.success(comment);
    // }
    
    // @PostMapping("/{commentId}/like")
    // @ApiOperation("点赞/取消点赞")
    // @PreAuthorize("hasRole('USER')")
    // public ApiResponse<Boolean> toggleLike(
    //         @ApiParam("评论ID") @PathVariable @NotNull(message = "评论ID不能为空") Long commentId) {
    //     Long userId = SecurityUtils.getCurrentUserId();
    //     boolean success = commentService.toggleLike(commentId, userId);
    //     return ApiResponse.success(success);
    // }
    
    // @GetMapping("/statistics/{productId}")
    // @ApiOperation("获取评论统计")
    // public ApiResponse<CommentStatisticsVO> getCommentStatistics(
    //         @ApiParam("商品ID") @PathVariable @NotNull(message = "商品ID不能为空") Long productId) {
    //     CommentStatisticsVO statistics = commentService.getCommentStatistics(productId);
    //     return ApiResponse.success(statistics);
    // }
} 